<?php

namespace app\admin\controller\device;

use think\facade\Db;
use app\common\controller\Backend;

class Lampligt extends Backend
{

    protected $noNeedLogin      = ['index','add','delete','edit'];//生成土壤墒情测试数据

    public function initialize()
    {
        parent::initialize();
        //$this->model = new WeacherDeviceDataModel();
    }


    /**
     * 照明列表
     */
    public function index()
    {
        $page = $this->request->param('page',1);
//        $status = $this->request->param('status','');
        $name = $this->request->param('name','');
        $id = $this->request->param('id','');

        $where = [];
        $where[] = ['a.is_delete','=',0];
//        if (!empty($status)) {
//            $where[] = ['a.status','=',$status];
//        }
        if (!empty($name)) {
            $where[] = ['a.name','like','%'.$name.'%'];
        }
        if (!empty($id)) {
            $where[] = ['a.id','=',$id];
        }

        $monitorList = Db::name('park_lamp_ligt')
            ->alias('a')
            ->field('a.*,b.name as lamp_name')
            ->leftJoin('park_lamp b','a.lamp_id = b.id')
            ->where($where)
            ->page($page,10)
            ->select();
        $count = Db::name('park_lamp_ligt')
            ->alias('a')
            ->field('a.*,b.name as lamp_name')
            ->leftJoin('park_lamp b','a.lamp_id = b.id')
            ->where($where)
            ->count();
        $this->success('请求成功',['list'=>$monitorList,'count'=>$count]);
    }

    /**
     * 添加照明设备
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $name = $this->request->post('name');
            $lampId = $this->request->post('lamp_id', '');
            if (empty($name) || empty($lampId)) {
                $this->error('缺少参数');
            }
            Db::name('park_lamp_ligt')->insert([
                'name' => $name,
                'lamp_id' => $lampId,
                'createtime' => date('Y-m-d H:i:s')
            ]);
            $this->success('添加成功');
        } else {
            $lampList = Db::name('park_lamp')->select();
            $this->success('获取成功',$lampList);
        }
    }

    /**
     * 删除照明设备
     */
    public function delete()
    {
        $id = $this->request->post('id');
        Db::name('park_lamp_ligt')->where('id',$id)->update(['is_delete'=>1]);
        $this->success('删除成功');
    }

    /**
     * 编辑照明设备
     */
    public function edit()
    {
        $id = $this->request->param('id');
        if ($this->request->isPost()) {
            $name = $this->request->post('name');
            $lampId = $this->request->post('lamp_id', '');
            if (empty($name)) {
                $this->error('缺少参数');
            }
            Db::name('park_lamp_ligt')->where('id', $id)->update([
                'name' => $name,
                'lamp_id' =>$lampId,
                'updatetime' => date('Y-m-d H:i:s'),
            ]);
            $this->success('编辑成功');
        } else {
            $lamp = Db::name('park_lamp_ligt')->where(['id'=>$id])->find();
            $this->success('请求成功',$lamp);
        }
    }
}